﻿/////////////////////////////////////////////////////////////////////
//
//	Map_FVG C# Class Library.
//
//	QuadroUnione3
//	
//
//	autore: Gustavo Mainardi 
//	versione: 1.0
//	data: 01/12/2015
//
//
//	Cronologia delle versioni:
//
//	Versione 1.0 01/12/2015
//		versione originale
//
//////////////////////////////////////////////////////////////////////////
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SQLite;

namespace Map_Fvg
{
    public partial class QuadroUnione3 : Form
    {
        Point P1 = new Point(681,546);
        Point P2 = new Point(72,546);
        Point P3 = new Point(72, 14);
        int ndx, ndy, ndivX, ndivY;
        string[] nFoglio = new string[30]
          { "017","018","000","000","000",
            "030","031","032","033","034",
            "047","048","049","050","000",
            "064","065","066","067","000",
            "085","086","087","088","000",
            "106","107","108","109","110" };

//        static private Thread thr;
  //      static private int ithr;

        static public string pathCreate;
        static public DialogResult dResult;
//        private static System.Timers.Timer bTimer;


        public QuadroUnione3()
        {
            InitializeComponent();

            Quadro3.elemento_selez = string.Empty;

            ndx = 5;
            ndy = 6;
            ndivX = (P1.X - P2.X) / ndx;
            ndivY = (P1.Y - P3.Y) / ndy;
            readFile();
            readFile1();
            readFile2();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            
            string fileX = FormIniz.quadroUnione5000;
            fileX = string.Concat(fileX, "Quadro25000.jpg");

            Graphics dc = this.panel1.CreateGraphics();

            Image newImage = Image.FromFile(fileX);
            Point ulCorner = new Point(0, 0);
            Rectangle destRect = new Rectangle(0, 0, panel1.Size.Width, panel1.Size.Height);

            dc.DrawImage(newImage, ulCorner);
             
        }

        private void panel1_MouseClick(object sender, MouseEventArgs e)
        {
            Point diff = P3;
            diff.X = e.X - diff.X;
            if (diff.X < 0) return;
            diff.Y = e.Y - diff.Y;
            int nx = diff.X / ndivX;
            int ny = diff.Y / ndivY;

            int ipnt = nx + ndx * ny;
            if (ipnt >= nFoglio.Length) return;
            if (nFoglio[ipnt].Contains("000")) return;
            int ex = diff.X - nx * ndivX;
            int ey = diff.Y - ny * ndivY;

            int ndx2 = ndivX / 2;
            int ndy2 = ndivY / 2;

            string ss = string.Empty;
            if (ex < ndx2)
            {
                if (ey < ndy2) ss = "4";
                else ss = "3";
            }
            else
            {
                if (ey < ndy2) ss = "1";
                else ss = "2";
            }

            string s1 = nFoglio[ipnt];
            Quadro3.codiceTavoletta = string.Concat(s1, ss);

            int i = 0;
            for (i = 0; i < nrec1; ++i)
            {
                string s2 = tavol[i].codice;
                if (!s2.Contains(Quadro3.codiceTavoletta)) continue;
                Quadro3.IDTavoletta = tavol[i].IDtavol;
                break;
            }

            if (i == nrec1) Quadro3.IDTavoletta = string.Empty;

            dResult = 0;    
            Quadro3 formQuadro3 = new Quadro3();

            formQuadro3.ShowDialog();
            
            if(dResult != 0) this.Close();

        }

        public struct elenco
        {
            public string codice;
            public string IDtavol;

        }
        public static elenco[] tavol = new elenco[1];
        public static elenco[] elem = new elenco[1];
        public static elenco[] sez = new elenco[1];
        public static int nrec, nrec1,nrec2;

        private void readFile()
        {

            using (SQLiteConnection conn = SetConnection1())
            {
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {
                    string ss1 = "select count(*) from QU_5000";

                    cmd.CommandText = ss1;
                    // tables:  QU_5000   QU_10000  QU_25000
                    //  campi:  codice nome  X1  Y1  X2  Y2  X3  Y3  X4  Y4

                    nrec = 0;
                    SQLiteDataReader reader1 = cmd.ExecuteReader();
                    if (reader1.HasRows)
                    {
                        while (reader1.Read())
                        {
                            nrec = reader1.GetInt32(0);
                        }
                    }
                    reader1.Close();
                    if (nrec == 0)
                    {
                        MessageBox.Show("non trovato alcun elemento");
                        return ;
                    }
                    elem = new elenco[nrec];

                    ss1 = "select codice, nome from QU_5000";

                    cmd.CommandText = ss1;
                    SQLiteDataReader reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {
       //                 for (int i = 0; i < nrec; ++i)
                        int i = 0;
                            while (reader.Read())
                            {
                                elem[i].codice = reader.GetString(0).ToString();
                                elem[i].IDtavol = reader.GetString(1).ToString();
                                ++i;
                            }
                        }

                    }
                conn.Close();

                }
            }


        private void readFile1()
        {

            using (SQLiteConnection conn = SetConnection1())
            {
                conn.Open();

                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {
                    string ss1 = "select count(*) from QU_25000";

                    cmd.CommandText = ss1;
                    // tables:  QU_5000   QU_10000  QU_25000
                    //  campi:  codice nome  X1  Y1  X2  Y2  X3  Y3  X4  Y4

                    nrec1 = 0;
                    SQLiteDataReader reader1 = cmd.ExecuteReader();
                    if (reader1.HasRows)
                    {
                        while (reader1.Read())
                        {
                            nrec1 = reader1.GetInt32(0);
                        }
                    }
                    reader1.Close();
                    if (nrec1 == 0)
                    {
                        MessageBox.Show("non trovato alcun elemento");
                        return;
                    }

                    tavol = new elenco[nrec1];
                    ss1 = "select codice, nome from QU_25000";

                    cmd.CommandText = ss1;
                    SQLiteDataReader reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {
                         int i = 0;
                //        for (int i = 0; i < nrec1; ++i)
                        {
                            while (reader.Read())
                            {
                                tavol[i].codice = reader.GetString(0).ToString();
                                tavol[i].IDtavol = reader.GetString(1).ToString();
                                ++i;
                            }
                        }

                    }

                }
                conn.Close();
            }
        }
        private void readFile2()
        {
            using (SQLiteConnection conn = SetConnection1())
            {
                conn.Open();

                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {
                    string ss1 = "select count(*) from QU_10000";

                    cmd.CommandText = ss1;
                    // tables:  QU_5000   QU_10000  QU_25000
                    //  campi:  codice nome  X1  Y1  X2  Y2  X3  Y3  X4  Y4

                    nrec2 = 0;
                    SQLiteDataReader reader1 = cmd.ExecuteReader();
                    if (reader1.HasRows)
                    {
                        while (reader1.Read())
                        {
                            nrec2 = reader1.GetInt32(0);
                        }
                    }
                    reader1.Close();
                    if (nrec2 == 0)
                    {
                        MessageBox.Show("non trovato alcun elemento");
                        return;
                    }

                    sez = new elenco[nrec2];

                    ss1 = "select codice, nome from QU_10000";

                    cmd.CommandText = ss1;
                    SQLiteDataReader reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {
                        int i = 0;
                        while (reader.Read())
                        {
                            sez[i].codice = reader.GetString(0).ToString();
                            sez[i].IDtavol = reader.GetString(1).ToString();
                            ++i;
                        }
                    }
                }
                conn.Close();
            }
        }
 
        private static SQLiteConnection SetConnection1()
        {
            string DataSource = "Data Source=";
            DataSource = string.Concat(DataSource, FormIniz.quadroUnione, ";");
            SQLiteConnection conn = new SQLiteConnection(DataSource);
            return (conn);
        }



        private void button1_Click(object sender, EventArgs e)
        {
            dResult = DialogResult.None;
            this.Close();
        }




 

    }
}
